// @import
import HeroCtrl from "./HeroCtrl";
import MyCoroutine from "../comm/base/MyCoroutine";

// @const
const SP_ANIM_ATTK = "Attk";
const SP_ANIM_STAND = "Stand";

/**
 * 执行攻击
 * 
 * @param SELF this 指针
 * @param out_oTargetUserIdArray ( 输出参数 ) 目标用户 Id 数组
 */
export function __doAttk(SELF: HeroCtrl, out_oTargetUserIdArray: Array<number> = null): void {
    if (null == SELF || 
        SELF._nCurrHP <= 0) {
        return;
    }

    // 停止协程
    MyCoroutine.stopCoroutine(SELF._strMoveTo_XCID);

    // 获取 Spine 骨骼控件
    let oSpSkeleton = SELF.node.getComponentInChildren(sp.Skeleton);
    // 获取当前动画
    let oTe0: sp.spine.TrackEntry = oSpSkeleton.getCurrent(0);

    if (null != oTe0 && 
        null != oTe0.animation) {
        if (SP_ANIM_ATTK != oTe0.animation.name) {
            // 强制结束前一动画
            oTe0.animationEnd = oTe0.animationLast;
            oTe0.loop = false;
        }
        else {
            return;
        }
    }

    cc.find("Holder_SoundGroup/AudioSource_Cut", SELF.node)
        .getComponent(cc.AudioSource)
        .play();

    // 设置攻击动画
    let oTe1: sp.spine.TrackEntry = oSpSkeleton.addAnimation(0, SP_ANIM_ATTK, false);
    oTe1.mixDuration = 0.1;

    // 攻击完成后切换到站立动画
    let oTe2: sp.spine.TrackEntry = oSpSkeleton.addAnimation(0, SP_ANIM_STAND, true);
    oTe2.mixDuration = 0.1;

    // 根据攻击范围节点构建一个攻击范围矩形
    let oAttkAreaNode: cc.Node = cc.find("Empty_AttkArea", SELF.node);
    let oAttkAreaAtWorldPos: cc.Vec2 = oAttkAreaNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
    let oAttkAreaRect: cc.Rect = cc.rect(
        oAttkAreaAtWorldPos.x - oAttkAreaNode.width / 2,
        oAttkAreaAtWorldPos.y - oAttkAreaNode.height / 2,
        oAttkAreaNode.width,
        oAttkAreaNode.height,
    );

    if (null == out_oTargetUserIdArray) {
        out_oTargetUserIdArray = [];
    }

    for (let oAnotherNode of SELF._oClosesetHeroNodeMap.values()) {
        if (null == oAnotherNode || 
            !oAnotherNode.isValid) {
            continue;
        }

        if (SELF.node.scaleX > 0) {
            // 如果是面向右方
            if (oAnotherNode.x < SELF.node.x) {
                continue;
            }
        }
        else {
            // 如果是面向左方
            if (oAnotherNode.x > SELF.node.x) {
                continue;
            }
        }

        // 获取对方的两个伤害点
        let oDmgPointNode_0: cc.Node = cc.find("Holder_DamagePointArray/Empty_Point_0", oAnotherNode);
        let oDmgPointNode_1: cc.Node = cc.find("Holder_DamagePointArray/Empty_Point_1", oAnotherNode);
        let oDmgWorldPos__0: cc.Vec2 = oDmgPointNode_0.convertToWorldSpaceAR(cc.Vec2.ZERO_R);
        let oDmgWorldPos__1: cc.Vec2 = oDmgPointNode_1.convertToWorldSpaceAR(cc.Vec2.ZERO_R);

        if (!oAttkAreaRect.contains(oDmgWorldPos__0) &&
            !oAttkAreaRect.contains(oDmgWorldPos__1)) {
            continue;
        }
        
        let oHeroCtrl: HeroCtrl = oAnotherNode.getComponent(HeroCtrl)

        if (null != oHeroCtrl && 
            oHeroCtrl._nCurrHP > 0) {
            out_oTargetUserIdArray.push(oHeroCtrl._nUserId);
        }
    }
}
